package predictionEngine;

import com.sun.jndi.ldap.EntryChangeResponseControl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Attribute {
    private String name;
    private double MAX,MIN;
    private boolean numeric = false;
    private HashMap<String,Integer> uniqueValues=null;//List the allowed categorical values. Null signifies a numeric attribute
    
    Attribute(String name, ArrayList<String> allowedValues) {
       this.name = name;
       if(allowedValues.size()>1){
           uniqueValues = new HashMap<String,Integer>();
            addAllowedValues(allowedValues);    
       }
       else if(allowedValues.get(0).startsWith("Date"))
           uniqueValues = new HashMap<String,Integer>();
    }
    
    public String getRealValue(double internalValue){
        Iterator ie = uniqueValues.entrySet().iterator();
        while(ie.hasNext()){
            Map.Entry<String,Integer> e = (Map.Entry<String,Integer>)ie.next();
            if(e.getValue()==(int)internalValue)
                return e.getKey();
        }
        return "error internal value" + internalValue + "could not be found";
    }
    
    public final void addAllowedValues(ArrayList<String> values){
       for(String value:values){
           addUniqueValue(value);
       }
    }
    
    private void addUniqueValue(String value) {
        if (!isNumeric()) {
            uniqueValues.put(value, uniqueValues.size());
        }
    }    
    
    public boolean isNumeric(){
        if(uniqueValues==null)
            return true;
        return false;
    }
    public int nrOfAllowedValues(){
        return uniqueValues.values().size();
    }
    
    public String getName(){
        return name;
    }
    
    
    //parses strings to double or transcode a nominal values ot a integer for internal usage. use translat to get the real value.
     public double getValue(String value){        
        if(isNumeric()){
            double d = Double.parseDouble(value);
            MAX = Math.max(MAX, d);
            MIN = Math.min(MIN, d);
            return d;
        }else{
            Integer internalValue = uniqueValues.get(value);
            if(internalValue==null)
                addUniqueValue(value);
            internalValue = uniqueValues.get(value);
            return internalValue.doubleValue();
        }
    }

    
    
}
